﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using WeChatSDK.Common;
using WeChatSDK.Common.Enums.Http;
using WeChatSDK.RequestComponent.Http;
using WeChatSDK.WeChatDto.CustomSerivce;
using WeChatSDK.WeChatDto.CustomSerivce.Message;
using WeChatSDK.WeChatDto.Request.Http;
using WeChatSDK.WeChatServiceComponent.Conversation.BaseSupport;

namespace WeChatSDK.WeChatServiceComponent.CustomService.KeyAccount
{
    /// <summary>
    /// 微信客服账号管理服务
    /// </summary>
    public class KeyAccountService
    {
        private readonly HttpHandle httpHandle;
        public KeyAccountService()
        {
            this.httpHandle = new HttpHandle();
        }
        #region 客服账号管理
        /// <summary>
        /// 添加客服账号
        /// </summary>
        /// <param name="weChatKeyAccountDto">微信客服账号信息</param>
        /// <returns></returns>
        public HttpResponseDto Add(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/add?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(weChatKeyAccountDto, WeChatGeneral.JsonSerializerSettings));
            return SendWebRequest(url, content, "application/json", MethodType.POST, FormatType.JSON);
        }
        /// <summary>
        /// 修改客服账号
        /// </summary>
        /// <param name="weChatKeyAccountDto">微信客服账号信息</param>
        /// <returns></returns>
        public HttpResponseDto Update(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/update?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(weChatKeyAccountDto, WeChatGeneral.JsonSerializerSettings));
            return SendWebRequest(url, content, "application/json", MethodType.POST, FormatType.JSON);
        }
        /// <summary>
        /// 删除客服账号
        /// </summary>
        /// <param name="weChatKeyAccountDto">微信客服账号信息</param>
        /// <returns></returns>
        public HttpResponseDto Delete(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/del?access_token={0}&kf_account={1}", new BaseSupportService().CreateAccessToken().AccessToken, weChatKeyAccountDto.KfAccount);
            return SendWebRequest(url, null, "application/json", MethodType.GET, FormatType.JSON);
        }
        /// <summary>
        /// 设置客服账号头像
        /// 头像图片文件必须是jpg格式，推荐使用640*640大小的图片以达到最佳效果
        /// </summary>
        /// <param name="weChatKeyAccountDto">微信客服账号信息</param>
        /// <returns></returns>
        public HttpResponseDto Avatar(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token={0}&kf_account={1}", new BaseSupportService().CreateAccessToken().AccessToken, weChatKeyAccountDto.KfAccount);
            byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(weChatKeyAccountDto, WeChatGeneral.JsonSerializerSettings));
            return SendWebRequest(url, content, "image/jpeg", MethodType.POST, FormatType.FormData);
        }
        /// <summary>
        /// 发送绑定客服微信邀请
        /// </summary>
        /// <param name="weChatKeyAccountDto"></param>
        /// <returns></returns>
        public HttpResponseDto SendBindingInvite(WeChatKeyAccountDto weChatKeyAccountDto)
        {
            if (string.IsNullOrWhiteSpace(weChatKeyAccountDto.InviteWx))
            {
                return new HttpResponseDto() { ResponseMessage = "被邀请绑定的客服微信账号不能为空。", IsError =true };
            }
            string url = string.Format("https://api.weixin.qq.com/customservice/kfaccount/inviteworker?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(weChatKeyAccountDto, WeChatGeneral.JsonSerializerSettings));
            return SendWebRequest(url, content, "application/json", MethodType.POST, FormatType.JSON);
        }
        /// <summary>
        /// 获取所有客服信息
        /// </summary>
        /// <returns></returns>
        public HttpResponseDto Get()
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/customservice/getkflist?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            return SendWebRequest(url, null, "application/json", MethodType.GET, FormatType.JSON);
        }
        #endregion

        #region 发送客服消息
        /// <summary>
        /// 客服发送消息
        /// </summary>
        /// <returns></returns>
        public HttpResponseDto SendMessage<T>(T weChatKeyAccountMessageDto) where T :WeChatKeyAccountMessageDto
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            byte[] content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(weChatKeyAccountMessageDto, WeChatGeneral.JsonSerializerSettings));
            return SendWebRequest(url, content, "application/json", MethodType.POST, FormatType.JSON);
        }
        #endregion

        private HttpResponseDto SendWebRequest(string url, byte[] content, string contentType, MethodType methodType, FormatType formatType)
        {
            HttpRequestDto httpRequestDto = new HttpRequestDto();
            httpRequestDto.Url = url;
            httpRequestDto.Headers.Add("Content-Type", contentType);
            httpRequestDto.Method = methodType;
            httpRequestDto.ContentType = formatType;
            if (content != null && content.Length > 0)
            {
                httpRequestDto.Content = content;
            }
            return httpHandle.WebSendRequest(httpRequestDto);
        }

    }
}
